/****************************************************************************
**
** Copyright (C) 2008 Nokia Corporation and/or its subsidiary(-ies).
** Contact: Qt Software Information (qt-info@nokia.com)
**
** This file is part of the example classes of the Qt Toolkit.
**
** Commercial Usage
** Licensees holding valid Qt Commercial licenses may use this file in
** accordance with the Qt Commercial License Agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Nokia.
**
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License versions 2.0 or 3.0 as published by the Free
** Software Foundation and appearing in the file LICENSE.GPL included in
** the packaging of this file.  Please review the following information
** to ensure GNU General Public Licensing requirements will be met:
** http://www.fsf.org/licensing/licenses/info/GPLv2.html and
** http://www.gnu.org/copyleft/gpl.html.  In addition, as a special
** exception, Nokia gives you certain additional rights. These rights
** are described in the Nokia Qt GPL Exception version 1.3, included in
** the file GPL_EXCEPTION.txt in this package.
**
** Qt for Windows(R) Licensees
** As a special exception, Nokia, as the sole copyright holder for Qt
** Designer, grants users of the Qt/Eclipse Integration plug-in the
** right for the Qt/Eclipse Integration to link to functionality
** provided by Qt Designer and its related libraries.
**
** If you are unsure which license is appropriate for your use, please
** contact the sales department at qt-sales@nokia.com.
**
****************************************************************************/

#include <math.h>
#include "ViewerWidget.h"

ViewerWidget::ViewerWidget(QWidget *parent, osgViewer::ViewerBase::ThreadingModel threadingModel)
    : QWidget(parent)
{
	setThreadingModel(threadingModel);

	QWidget* widget1 = addViewWidget( createCamera(0,0,width()/3,height()/3), osgDB::readNodeFile("cow.osgt") );
	//QWidget* widget2 = addViewWidget( createCamera(0,0,width()/3,height()/3), osgDB::readNodeFile("glider.osgt") );
	//QWidget* widget3 = addViewWidget( createCamera(0,0,width()/3,height()/3), osgDB::readNodeFile("axes.osgt") );
	//QWidget* widget4 = addViewWidget( createCamera(0,0,width()/3,height()/3), osgDB::readNodeFile("fountain.osgt") );
	//QWidget* popupWidget = addViewWidget( createCamera(900,100,320,240,"Popup window",true),
	//	osgDB::readNodeFile("axes.osgt") );
	//popupWidget->show();

	QGridLayout* grid = new QGridLayout;
	grid->setSpacing(0);
	grid->setMargin(1);
	grid->addWidget( widget1, 0, 0 );
	//grid->addWidget( widget2, 0, 1 );
	//grid->addWidget( widget3, 1, 0 );
	//grid->addWidget( widget4, 1, 1 );
	setLayout( grid );

	connect( &_timer, SIGNAL(timeout()), this, SLOT(update()) );
	_timer.start( 10 );
}

ViewerWidget::~ViewerWidget()
{
}

QWidget* ViewerWidget::addViewWidget( osg::Camera* camera, osg::Node* scene )
{
	osgViewer::View* view = new osgViewer::View;
	//view->setUpViewInWindow()	
	view->setCamera( camera );
	addView( view );

	view->setSceneData( scene );
	scene->getOrCreateStateSet()->setMode(GL_DEPTH_TEST, osg::StateAttribute::ON);
	view->addEventHandler( new osgViewer::StatsHandler );
	view->setCameraManipulator( new osgGA::TrackballManipulator );

	osgQt::GraphicsWindowQt* gw = dynamic_cast<osgQt::GraphicsWindowQt*>( camera->getGraphicsContext() );
	return gw ? gw->getGLWidget() : NULL;
}

osg::Camera* ViewerWidget::createCamera( int x, int y, int w, int h, const std::string& name, bool windowDecoration)
{
	osg::DisplaySettings* ds = osg::DisplaySettings::instance().get();
	osg::ref_ptr<osg::GraphicsContext::Traits> traits = new osg::GraphicsContext::Traits;
	traits->windowName = name;
	traits->windowDecoration = windowDecoration;
	traits->x = x;
	traits->y = y;
	traits->width = w;
	traits->height = h;
	traits->doubleBuffer = true;
	traits->alpha = ds->getMinimumNumAlphaBits();
	traits->stencil = ds->getMinimumNumStencilBits();
	traits->sampleBuffers = ds->getMultiSamples();
	traits->samples = ds->getNumMultiSamples();

	osg::ref_ptr<osg::Camera> camera = new osg::Camera;
	camera->setGraphicsContext( new osgQt::GraphicsWindowQt(traits.get()) );

	camera->setClearColor( osg::Vec4(0.2, 0.2, 0.6, 1.0) );
	camera->setViewport( new osg::Viewport(0, 0, traits->width, traits->height) );
	camera->setProjectionMatrixAsPerspective(
		30.0f, static_cast<double>(traits->width)/static_cast<double>(traits->height), 1.0f, 10000.0f );
	return camera.release();
}

void ViewerWidget::paintEvent( QPaintEvent* event )
{ 
	frame(); 
}



